package com.gitee.zhanyao.cloud.mqtest.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.bus.BusProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MqController {

    @Autowired
    private ApplicationEventPublisher publisher;

    @Autowired
    private BusProperties busProperties;

    /**
     * Publish the {@link UserRemoteApplicationEvent}.
     * @param name the user name
     * @param destination the destination
     * @return If published
     */
    @GetMapping("/bus/event/publish/user")
    public boolean publish(@RequestParam String name, @RequestParam(required = false) String destination) {
        User user = new User();
        user.setId(System.currentTimeMillis());
        user.setName(name);
        publisher.publishEvent(new UserRemoteApplicationEvent(this, user, busProperties.getId(), destination));
        return true;
    }

}
